package com.itheima.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import com.itheima.bos.domain.take_delivery.WayBill;
import com.itheima.bos.service.take_delivery.IWayBillService;
import com.itheima.bos.web.action.common.BaseAction;

public class WayBillAction extends BaseAction<WayBill> {

	// 注入waybillservice
	@Autowired
	private IWayBillService wayBillService;

	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String sava() {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			// 去除有没有order的id
			if (model.getOrder() != null
					&& (model.getOrder().getId() == null || model.getOrder()
							.getId() == 0)) {
				model.setOrder(null);
			}
			// 调用业务层保存数据
			wayBillService.save(model);
			// 保存成功

			result.put("success", true);
			result.put("msg", "保存运单成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "保存运单失败!");
		} finally {
			pushPageDataToValueStack(result);
		}
		return SUCCESS;
	}

	// 不太条件分页查询
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 封装查询条件
		Pageable pageable = new PageRequest(page - 1, rows);
		// 查询
		Page<WayBill> pageData = wayBillService.pageQuery(model,pageable);
		// 获取到的数据压入值栈
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	// 根据运单号查询运单
	@Action(value = "waybill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {
		Map<String, Object> result = new HashMap<String, Object>();
		// 掉用业务层，根据运单号查询运单
		WayBill wayBill=wayBillService.findByWayBillNum(model.getWayBillNum());
		// 判断订单是否存在
		if (wayBill == null) {
			result.put("success", false);
		} else {
			result.put("success", true);
			result.put("wayBillData", wayBill);
		}
		pushPageDataToValueStack(result);
		return SUCCESS;
	}
}
